package com.newlink.building.userinfo.activity

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.newlink.building.common_base.base.NL_BaseMvpActivity
import com.newlink.building.common_base.widget.SpaceItemDecoration
import com.newlink.building.userinfo.R
import com.newlink.building.userinfo.adapter.Module_UserInfo_CallSettingCallPreferenceAdapter
import com.newlink.building.userinfo.adapter.Module_UserInfo_CallSettingCallPreferenceDialogAdapter
import com.newlink.building.userinfo.bean.Module_UserInfo_CallSettingCallPreferenceDialogBean
import com.newlink.building.userinfo.databinding.ActivityCallSettingCallPreferenceBinding
import com.newlink.building.userinfo.mvp.Module_UserInfo_CallSettingCallPreferencePresenter
import com.newlink.building.userinfo.mvp.contract.Module_UserInfo_CallSettingCallPreferenceContract

/**
 * @author wangyang
 * @date 2022/10/17
 * @description 我的——通话设置——呼叫偏好页面
 */
class Module_UserInfo_CallSettingCallPreferenceActivityNL :
    NL_BaseMvpActivity<Module_UserInfo_CallSettingCallPreferenceContract.View, Module_UserInfo_CallSettingCallPreferenceContract.Presenter>(),
    Module_UserInfo_CallSettingCallPreferenceContract.View, View.OnClickListener {

    private lateinit var mCallSettingCallPreferenceAdapter: Module_UserInfo_CallSettingCallPreferenceAdapter
    private lateinit var mCallStateList: ArrayList<Module_UserInfo_CallSettingCallPreferenceDialogBean>
    private lateinit var mRingTimeList: ArrayList<Module_UserInfo_CallSettingCallPreferenceDialogBean>
    private lateinit var mPhoneList: ArrayList<Module_UserInfo_CallSettingCallPreferenceDialogBean>
    private lateinit var mChosePhoneList: ArrayList<Module_UserInfo_CallSettingCallPreferenceDialogBean>
    private var mPhoneListClickPosition: Int = 0

//    override fun attachLayoutRes(): Int {
//        return R.layout.activity_call_setting_call_preference
//    }

    private lateinit var mBinding: ActivityCallSettingCallPreferenceBinding
    override fun attachLayoutRes(): View {
        mBinding = ActivityCallSettingCallPreferenceBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initData() {
        mCallStateList =
            arrayListOf<Module_UserInfo_CallSettingCallPreferenceDialogBean>(
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    getString(R.string.call_setting_call_preferences_state),
                    getString(R.string.call_setting_call_preferences_state_group),
                    true
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    getString(R.string.call_setting_call_preferences_state),
                    getString(R.string.call_setting_call_preferences_state_order),
                    false
                )
            )

        mRingTimeList =
            arrayListOf<Module_UserInfo_CallSettingCallPreferenceDialogBean>(
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    getString(R.string.call_setting_call_preferences_ring_time),
                    "5s",
                    true
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    getString(R.string.call_setting_call_preferences_ring_time),
                    "10s",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    getString(R.string.call_setting_call_preferences_ring_time),
                    "15s",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    getString(R.string.call_setting_call_preferences_ring_time),
                    "20s",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    getString(R.string.call_setting_call_preferences_ring_time),
                    "25s",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    getString(R.string.call_setting_call_preferences_ring_time),
                    "30s",
                    false
                )
            )

        mPhoneList =
            arrayListOf<Module_UserInfo_CallSettingCallPreferenceDialogBean>(
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "号码1",
                    "",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "号码2",
                    "",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "号码3",
                    "",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "号码4",
                    "",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "号码5",
                    "",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "号码6",
                    "",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "号码7",
                    "",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "号码8",
                    "",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "号码9",
                    "",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "号码10",
                    "",
                    false
                )
            )
        mChosePhoneList =
            arrayListOf<Module_UserInfo_CallSettingCallPreferenceDialogBean>(
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "13600000001",
                    "1001",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "13600000002",
                    "1002",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "13600000003",
                    "1003",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "13600000004",
                    "1004",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "13600000005",
                    "1005",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "13600000006",
                    "1006",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "13600000007",
                    "1007",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "13600000008",
                    "1008",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "13600000009",
                    "1009",
                    false
                ),
                Module_UserInfo_CallSettingCallPreferenceDialogBean(
                    "13600000010",
                    "1010",
                    false
                )
            )
    }

    override fun initView() {
        findViewById<TextView>(R.id.tv_title_bar_center).text =
            resources.getText(R.string.call_setting_call_preferences)
        mBinding.rlCallState.setOnClickListener(this)
        mBinding.rlRingTime.setOnClickListener(this)

        initAdapter(mPhoneList)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.rl_call_state -> {
                //呼叫类型
                showBottomDialog(
                    this,
                    mCallStateList,
                    getString(R.string.call_setting_call_preferences_state)
                )
            }

            R.id.rl_ring_time -> {
                //响铃时间
                showBottomDialog(
                    this,
                    mRingTimeList,
                    getString(R.string.call_setting_call_preferences_ring_time)
                )
            }
        }

    }

    override fun createPresenter(): Module_UserInfo_CallSettingCallPreferenceContract.Presenter {
        return Module_UserInfo_CallSettingCallPreferencePresenter()
    }

    override fun showData() {
    }

    private fun initAdapter(list: ArrayList<Module_UserInfo_CallSettingCallPreferenceDialogBean>) {
        mBinding.rvPhone.setHasFixedSize(true)
        val linearLayoutManager: LinearLayoutManager = object : LinearLayoutManager(this) {
            //禁止竖向滑动 RecyclerView 为垂直状态（VERTICAL）
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        mBinding.rvPhone.layoutManager = linearLayoutManager
        mBinding.rvPhone.isNestedScrollingEnabled = false

        mCallSettingCallPreferenceAdapter = Module_UserInfo_CallSettingCallPreferenceAdapter(list)
        mCallSettingCallPreferenceAdapter?.addChildClickViewIds(
            R.id.ll
        )
        mCallSettingCallPreferenceAdapter?.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.ll -> {
                    mPhoneListClickPosition = position
                    showBottomDialog(
                        this,
                        mChosePhoneList,
                        getString(R.string.call_setting_call_preferences_chose_phone)
                    )
                }
            }
        }

        mBinding.rvPhone.adapter = mCallSettingCallPreferenceAdapter
    }

    /**
     * 底部弹窗
     * 包括：呼叫类型、响铃时间、选择号码
     */
    private fun showBottomDialog(
        context: Context,
        data: MutableList<Module_UserInfo_CallSettingCallPreferenceDialogBean>,
        title: String
    ) {
        val bottomSheetDialog = BottomSheetDialog(context, R.style.BottomSheetDialog)
        bottomSheetDialog.setCanceledOnTouchOutside(false)
        val view =
            LayoutInflater.from(context).inflate(R.layout.dialog_call_preference, null, false)
        val recyclerView = view.findViewById(R.id.recyclerView) as RecyclerView
        view.findViewById<TextView>(R.id.tv_cancel).setOnClickListener {
            bottomSheetDialog.dismiss()
        }
        view.findViewById<TextView>(R.id.tv_title).text = title
        view.findViewById<TextView>(R.id.tv_confirm).setOnClickListener {
            for (tempData in data) {
                if (tempData.isChecked) {
                    if (title == getString(R.string.call_setting_call_preferences_state)) {
                        //呼叫类型
                        mBinding.tvCallState.text = tempData.content

                        if (mBinding.tvCallState.text == getString(R.string.call_setting_call_preferences_state_group)) {
                            //群呼
                            mBinding.rlRingTime.visibility = View.GONE
                            mBinding.rvPhone.visibility = View.GONE
                        } else if (mBinding.tvCallState.text == getString(R.string.call_setting_call_preferences_state_order)) {
                            //按顺序
                            mBinding.rlRingTime.visibility = View.VISIBLE
                            mBinding.rvPhone.visibility = View.VISIBLE
                        }

                        for (tempCallState in mCallStateList) {
                            tempCallState.isChecked =
                                tempCallState.content == mBinding.tvCallState.text
                        }
                    } else if (title == getString(R.string.call_setting_call_preferences_ring_time)) {
                        //响铃时间
                        mBinding.tvRingTime.text = tempData.content

                        for (tempRingTime in mRingTimeList) {
                            tempRingTime.isChecked =
                                tempRingTime.content == mBinding.tvRingTime.text
                        }
                    } else if (title == getString(R.string.call_setting_call_preferences_chose_phone)) {
                        //选择号码
                        mPhoneList.get(mPhoneListClickPosition).name =
                            tempData.name
                        mPhoneList.get(mPhoneListClickPosition).content =
                            tempData.content
                        mCallSettingCallPreferenceAdapter.notifyDataSetChanged()
                    }
                }
            }

            bottomSheetDialog.dismiss()
        }
        val mAdapter = Module_UserInfo_CallSettingCallPreferenceDialogAdapter(data)
        recyclerView.adapter = mAdapter
        recyclerView.addItemDecoration(SpaceItemDecoration(context))
        mAdapter?.setOnItemChildClickListener { adapter, view, position ->
            for ((index, item) in data.withIndex()) {
                item.isChecked = index == position
            }

            mAdapter.notifyDataSetChanged()
        }

        bottomSheetDialog.setContentView(view)
        bottomSheetDialog.show()
    }
}